# Maintainer: Rafal Brzegowy <rafal.brzegowy@yahoo.com>
# Contributor: Raed Rizqie <raed.rizqie@gmail.com>

_realname=precice
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.5.0
pkgrel=4
pkgdesc='A Coupling Library for Partitioned Multi-Physics Simulations on Massively Parallel Systems (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
depends=("${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-msmpi"
         "${MINGW_PACKAGE_PREFIX}-petsc"
         "${MINGW_PACKAGE_PREFIX}-python-numpy")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             #"${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-petsc-build"
             "${MINGW_PACKAGE_PREFIX}-eigen3"
             "git")
license=('spdx:LGPL-3.0-or-later')
url='https://www.precice.org'
source=(https://github.com/precice/precice/archive/v${pkgver}.tar.gz
        001-fix-libprecice-dll-installation.patch)
sha256sums=('76ec6ee0d1a66f6f3d3d2d11f03cfc5aa7ef4d9e5deb9b7a4b4455ec7f796c00'
            '49fe04f9e72d1034b8909a7b593ccf42eeaf5203d9188f71706260948a0fb566')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd ${_realname}-${pkgver}
  apply_patch_with_msg \
    001-fix-libprecice-dll-installation.patch
}

build() {
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  . ${MINGW_PREFIX}/src/petsc-*/petsc
  for build in ${petsc_builds}; do
    #export PETSC_ARCH=${build}
    export PETSC_ARCH=dmo
    export PETSC_DIR=${MINGW_PREFIX}/src/petsc-${petsc_pkgver}
  done

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    CXXFLAGS+=" -Wno-c++11-narrowing"
  else
    CXXFLAGS+=" -Wno-narrowing"
  fi

  #Static Build
  mkdir -p "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G"MSYS Makefiles" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_CXX_STANDARD_LIBRARIES=-lws2_32 \
      -DBUILD_SHARED_LIBS=OFF \
      -DBoost_USE_STATIC_LIBS=ON \
      -DBUILD_TESTING=OFF \
      ${extra_config[@]} \
      -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
      -DPRECICE_MPICommunication=ON \
      -DPRECICE_PETScMapping=ON \
      -DPRECICE_PythonActions=ON \
      -DPRECICE_BUILD_TOOLS=OFF \
      -DPC_PETSc_INCLUDE_DIRS="$PREFIX_WIN/src/petsc-${petsc_pkgver}/include;$PREFIX_WIN/src/petsc-${petsc_pkgver}/$PETSC_ARCH/include" \
      -Wno-dev \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .

  #Shared Build
  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G"MSYS Makefiles" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_CXX_STANDARD_LIBRARIES=-lws2_32 \
      -DBUILD_SHARED_LIBS=ON \
      -DBUILD_TESTING=OFF \
      ${extra_config[@]} \
      -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
      -DPRECICE_MPICommunication=ON \
      -DPRECICE_PETScMapping=ON \
      -DPRECICE_PythonActions=ON \
      -DPC_PETSc_INCLUDE_DIRS="$PREFIX_WIN/src/petsc-${petsc_pkgver}/include;$PREFIX_WIN/src/petsc-${petsc_pkgver}/$PETSC_ARCH/include" \
      -Wno-dev \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  #Static Install
  cd "${srcdir}/build-${MSYSTEM}-static"
  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake --install .

  #Shared Install
  cd "${srcdir}/build-${MSYSTEM}-shared"
  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake --install .

  # add ".exe" extension to binprecice
  mv "${pkgdir}"${MINGW_PREFIX}/bin/binprecice{,.exe}

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
